<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>从相邻元素对还原数组</title>
</head>
<body>
  <script>
    function fn(pairs) {
      const ans = [], map = {};
      // 建图
      for (const [p, q] of pairs) {
        (map[p] = map[p] || []).push(q);
        (map[q] = map[q] || []).push(p);
      }
      // 找叶节点
      let p, q;
      for (const f in map) {
        if (map[f].length === 1) {
          ans.push(p = +f);
          q = map[f][0];
          break;
        }
      }
      // 遍历
      while (q !== undefined) {
        ans.push(q);
        [p, q] = [q, map[q][+(map[q][0] === p)]]
      }
      return ans
    }
    console.log(fn([[2, 1], [3, 4], [3, 2]]))
  </script>
</body>
</html>